package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.ExhibitionDetailsEntity;
import com.zw.yzk.kabu.data.network.task.CollectExhibitionTask;
import com.zw.yzk.kabu.data.network.task.ExhibitionDetailsTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.mapper.ExhibitionDetailsMapper;
import com.zw.yzk.kabu.view.view.ExhibitionDetailsView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/8.
 */

public class ExhibitionDetailsPresenter implements Presenter {

    private UseCase getExhibitionDetails;
    private UseCase collectExhibition;
    private AppCompatActivity activity;
    private ExhibitionDetailsMapper mapper;
    private ExhibitionDetailsView detailsView;

    @Inject
    public ExhibitionDetailsPresenter(@Named("exhibitionDetailsUseCase") UseCase getExhibitionDetails,
                                      @Named("collectExhibitionCosUseCase") UseCase collectExhibition,
                                      AppCompatActivity activity,
                                      ExhibitionDetailsMapper mapper) {
        this.getExhibitionDetails = getExhibitionDetails;
        this.collectExhibition = collectExhibition;
        this.mapper = mapper;
        this.activity = activity;
    }

    public void setView(ExhibitionDetailsView view) {
        this.detailsView = view;
    }

    public void getExhibitionDetails(int exhibitionId) {
        getExhibitionDetails.execute(new GetExhibitionDetails(activity), new ExhibitionDetailsTask(exhibitionId));
    }

    public void collectExhibition(int exhibitionId, boolean collect) {
        collectExhibition.execute(new CollectSubscriber(activity), new CollectExhibitionTask(exhibitionId, collect ? 1 : 0));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getExhibitionDetails.unSubscribe();
        collectExhibition.unSubscribe();
    }

    private class CollectSubscriber extends BaseSubscriber<Boolean> {

        public CollectSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean entity) {
            super.onNext(entity);
            ToastManager.getInstance().showToast(activity,
                    activity.getResources().getString(!detailsView.getCollectStatus() ? R.string.cancel_success : R.string.collect_success));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            detailsView.collectFailed();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(!detailsView.getCollectStatus() ? R.string.collect_failed : R.string.cancel_failed);
        }
    }

    private class GetExhibitionDetails extends BaseSubscriber<ExhibitionDetailsEntity> {


        public GetExhibitionDetails(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
        }

        @Override
        public void onNext(ExhibitionDetailsEntity exhibitionDetailsEntity) {
            super.onNext(exhibitionDetailsEntity);
            detailsView.getExhibitionDetailsSuccess(mapper.transfer(exhibitionDetailsEntity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_exhibition_details_failed);
        }
    }
}
